package com.hengyi.webcrawler.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.SecureUtil;
import com.hengyi.webcrawler.entity.Match;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author :  dongzp
 * Time :    2023/10/26 11:59
 * Project : WebCrawler
 * Email :   90fanhua@gmail.com
 * Remarks :
 **/
public class CommonUtils {

    public static String parseIssue(String url){
        String[] u = url.split("/");
        String qh = u[u.length - 1];
        return qh.split("_")[0];
    }

    public static String parseDay(String url){
        url = url.replace(".html","");
        String[] u = url.split("/");
        String qh = u[u.length - 1];
        String[] qhArr = qh.split("_");
        return qhArr[1] + "-" + qhArr[2];
    }

    public static void setDate(String url,Match match){
        String qh = parseDay(url);
        String date = DateUtil.formatDate(new Date());
        if(date.contains(qh)){
            match.setDate(date);
        }
    }

    /**
     * 计算唯一比赛ID
     * @param match
     */
    public static String getMatchId(Match match){
        match.setHome(match.getHome().trim());
        match.setAway(match.getAway().trim());
        String id = SecureUtil.md5(match.getHome() + match.getAway() + match.getIssue() + match.getIssueNum());
        return id;
    }

    /**
     * 设置状态
     * @param s
     * @return
     */
    public static int getStatus(String s){
        if(s.equals("已开奖")){
            return 4;
        }else if (s.equals("销售中")){
            return 1;
        }else if (s.equals("已停售")){
            return 3;
        }else {
            return 2;
        }
    }

    /**
     * 处理比分
     * @param match
     * @param bf
     */
    public static void disposeBf(Match match,String bf){
        if(StringUtils.isBlank(bf)){
            return;
        }
        if(match.getSellStatus() == 4){
            if(bf.contains("其它")){
                match.setHomeScore(0);
                match.setAwayScore(0);
            }else {
                String[] bfs = bf.split(":");
                match.setHomeScore(Integer.parseInt(bfs[0].trim()));
                match.setAwayScore(Integer.parseInt(bfs[1].trim()));
            }
            match.setBfResult(bf);
        }
    }

    /**
     * 解析比赛
     * @param data
     * @return
     */
    public static List<Match> parseGameXmlData(Integer issue,String data){
        List<Match> matchList = new ArrayList<>();
        try {
            Match match = null;
            Document document = XmlUtil.parseXml(data);
            Element info = document.getDocumentElement();
            Element matchEsp = XmlUtil.getElement(info,"matchesp");
            NodeList matchInfoList = matchEsp.getElementsByTagName("matchInfo");
            for(int i = 0; i < matchInfoList.getLength();i++){
                Element matchInfo = (Element) matchInfoList.item(i);
                Element matchElemList = XmlUtil.getElement(matchInfo,"matchelem");
                NodeList item = matchElemList.getElementsByTagName("item");
                for(int z = 0; z < item.getLength();z++){
                    match = new Match();
                    Element itemElem = (Element) item.item(z);
                    Element issueNum = XmlUtil.getElement(itemElem,"no");
                    Element home = XmlUtil.getElement(itemElem,"host");
                    Element away = XmlUtil.getElement(itemElem,"guest");
                    Element soccer = XmlUtil.getElement(itemElem,"soccer");
                    Element sellStatus = XmlUtil.getElement(itemElem,"matchandstate");
                    Element matchTime = XmlUtil.getElement(itemElem,"endTime");
                    String bf = soccer.getTextContent();
                    match.setIssue(issue);
                    match.setIssueNum(Integer.parseInt(issueNum.getTextContent()));
                    match.setHome(home.getTextContent());
                    match.setAway(away.getTextContent());
                    match.setBfResult(bf);
                    match.setSellStatus(CommonUtils.getStatus(sellStatus.getTextContent()));
                    Date mTime = DateUtil.parse(matchTime.getTextContent(),"yyyy-MM-dd HH:mm:ss");
                    match.setMatchTime(mTime);
                    String matchId = CommonUtils.getMatchId(match);
                    match.setId(matchId);
                    CommonUtils.disposeBf(match,bf);
                    matchList.add(match);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return matchList;
    }

    public static void main(String[] args) {
//       List<Match> matchList = parseGameXmlData(1234);
//       System.out.println(matchList);
    }
}
